﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _7DaysToDieTools.Class;

namespace _7DaysToDieTools
{
    public partial class FormMods : Form
    {
        #region 变量定义

        #endregion

        #region 初始化
        /// <summary>
        /// 初始化MOD管理窗口
        /// </summary>
        /// <param name="pForm">父窗口</param>
        public FormMods(Form pForm)
        {
            InitializeComponent();
            this.Location = new Point(pForm.Location.X + pForm.Width / 2 - this.Width / 2, pForm.Location.Y + pForm.Height / 2 - this.Height / 2);
        }

        private void FormMods_Load(object sender, EventArgs e)
        {

        }

        private void FormMods_Shown(object sender, EventArgs e)
        {
            rtboxMsg.Clear();
            rtboxMsg.AppendText("请手动点击<刷新>来加载MOD列表");
            
        }
        #endregion

        #region 方法集
        /// <summary>
        /// 加载MOD列表
        /// </summary>
        private void LoadModsList()
        {

        }
        #endregion

        #region 工具栏事件
        /// <summary>
        /// 加载或刷新MOD列表
        /// </summary>
        private void buttLoad_Click(object sender, EventArgs e)
        {
            cboxMods.Items.Clear();
            List<string> tmpList = ModsAction.LoadModNames();
            foreach (string item in tmpList)
            {
                cboxMods.Items.Add(item);
            }
            if (cboxMods.Items.Count > 0)
            {
                cboxMods.SelectedIndex = 0;
                cboxMods_SelectedIndexChanged(null, null);
            }
            else
            {
                cboxMods.SelectedIndex = -1;
                rtboxMsg.Clear();
                rtboxMsg.AppendText("未找到MOD");
            }
        }

        private void buttSwitch_Click(object sender, EventArgs e)
        {
            try
            {
                if (cboxMods.SelectedIndex == -1)
                {
                    return;
                }
                string modName = cboxMods.SelectedItem.ToString();
                if (cboxMods.SelectedIndex == 0)
                {
                    ModsAction.SwitchModDefault(rtboxMsg);
                }
                else
                {
                    ModsAction.SwitchModDefault(rtboxMsg);

                    if (ModsAction.HaveLocalMod(modName))
                    {
                        if (MessageBox.Show("检测到本地存在此MOD的压缩包，是否从本地解压并切换？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            ModsAction.DecMod(modName, rtboxMsg);
                        }
                        else
                        {
                            ModsAction.DownMod(modName, rtboxMsg);
                        }
                    }
                    else
                    {
                        ModsAction.DownMod(modName, rtboxMsg);
                    }

                    ModsAction.SwitchMod(modName, rtboxMsg);
                }
            }
            catch(Exception x)
            {
                MessageBox.Show("切换MOD出错：\n" + x.Message);
            }
        }

        private void cboxMods_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboxMods.SelectedIndex == -1) return;
            rtboxMsg.Clear();
            rtboxMsg.AppendText(cboxMods.SelectedItem.ToString() + "\n");
            rtboxMsg.AppendText("===================================================\n");
            rtboxMsg.AppendText(ModsAction.GetModInfo(cboxMods.SelectedItem.ToString()) + "\n");
        }
        #endregion
    }
}
